package com.coolt.client.components;

import com.coolt.client.Buttons;
import com.coolt.client.Combos;
import com.coolt.client.CooltRPC;
import com.coolt.client.CooltRPCAsync;
import com.coolt.client.model.Item;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class Search extends HorizontalPanel {

	private Buttons buttonsConstants;
	private Combos combosConstants;
	private TextBox txtSearch = new TextBox();
	private ListBox comboTipoItem = new ListBox();
	private Button btnSearch = new Button();

	public Search() {

		buttonsConstants = GWT.create(Buttons.class);
		combosConstants = GWT.create(Combos.class);

		setSpacing(8);
		setHorizontalAlignment(ALIGN_LEFT);
		setVerticalAlignment(ALIGN_MIDDLE);
		setHeight("90");

		txtSearch.setWidth("250");
		txtSearch.setFocus(true);

		comboTipoItem.setVisibleItemCount(1);
		comboTipoItem.addItem(combosConstants.tipoItemMovie(), Integer
				.toString(Item.TIPOITEM_FILMES));
		comboTipoItem.addItem(combosConstants.tipoItemBook(), Integer
				.toString(Item.TIPOITEM_LIVROS));
		comboTipoItem.addItem(combosConstants.tipoItemShow(), Integer
				.toString(Item.TIPOITEM_SHOWS));
		comboTipoItem.setSelectedIndex(0);
		comboTipoItem.setStyleName("listBox");

		btnSearch.setText(buttonsConstants.buttonSearch());

		add(txtSearch);
		add(comboTipoItem);
		add(btnSearch);

		final AsyncCallback callback = new AsyncCallback() {
			public void onSuccess(Object result) {
				// searchResult
			}

			public void onFailure(Throwable caught) {
				// lblServerReply.setText("Communication failed");
			}
		};

		btnSearch.addClickListener(new ClickListener() {
			public void onClick(Widget w) {
				// getService().myMethod(txtUserInput.getText(), callback);
			}
		});
	}

	public static CooltRPCAsync getService() {

		CooltRPCAsync service = (CooltRPCAsync) GWT.create(CooltRPC.class);
		ServiceDefTarget endpoint = (ServiceDefTarget) service;
		String moduleRelativeURL = GWT.getModuleBaseURL() + "cooltRPC";
		endpoint.setServiceEntryPoint(moduleRelativeURL);
		return service;
	}
}
